package com.jlkf.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.github.pagehelper.PageHelper;
import com.jlkf.dao.OrderItemsMapper;
import com.jlkf.dao.StarRemarkMapper;
import com.jlkf.dao.UserMapper;
import com.jlkf.dao.UserMessageMapper;
import com.jlkf.pojo.OrderItems;
import com.jlkf.pojo.StarRemark;
import com.jlkf.pojo.User;
import com.jlkf.pojo.UserMessage;
import com.jlkf.service.StarRemarkService;
import com.jlkf.service.util.FireBaseUtil;
import com.jlkf.service.util.LanguageUtils;
import com.jlkf.service.util.SendPushNotification;
import com.jlkf.service.util.StringTools;
import com.platform.server.response.pojo.ReturnData;
@Service
public class StarRemarkServiceImpl implements StarRemarkService{

	@Autowired
	private StarRemarkMapper starRemarkMapper;
	@Autowired
	private OrderItemsMapper orderItemsMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UserMessageMapper userMessageMapper;
	
	//评价之后 该订单评价的状态也需要修改为已评价
	@Override
	public ReturnData addUserAppraise(String json) {
		StarRemark startRemark = StringTools.json2Obj(json, StarRemark.class);
		if(startRemark==null){
			return new ReturnData(201,LanguageUtils.returnLan(1, "list_empty"));
		}
		Map<String,Object> map = new HashMap<>();
		int code=200;String msg="success"; 
		map.put("orderId", startRemark.getOrderId());
		map.put("type", 2);//用户评价
		 List<StarRemark> list=starRemarkMapper.getStarRemarkList(map);
		 if(list.size()>0){//已结被评价了
			 return new ReturnData(207,"The order has been reviewed. Please contact support for assistance.");
		 }
		try {
			OrderItems oi=orderItemsMapper.selectByPrimaryKey(startRemark.getOrderId());
			oi.setCommentStatus(1);
			oi.setStatus(9);//已完成(已评价)
			startRemark.setCreateTime(new Date());
			startRemark.setByEvaluation(oi.getDirberId());//被评价人
			int num=starRemarkMapper.insertSelective(startRemark);
			if(num==0){
				code=203; msg="fail"; 
			}else{
				int n= orderItemsMapper.updateByPrimaryKeySelective(oi);//订单更新为已评价
				if(n==0){
					code=202; msg="fail"; 
				}
				if(startRemark.getStar()<4){
					User u = userMapper.selectByPrimaryKey(oi.getDirberId());
//					FireBaseUtil.sendMessageAlert(u.getUseEquipment(), "You received a 3-star review.");
//					FireBaseUtil.postDataForFireBaseDataBase(u.getId()+"", "{\"orderId\":"+startRemark.getOrderId()+",\"status\":0,\"type\":2}");
//					SendPushNotification.sendMessageAlertDriver(u.getUseEquipment(), "You received a 3-star review.");
//					SendPushNotification.sendMessageSilentDriver(u.getUseEquipment(), "{\"orderId\":"+startRemark.getOrderId()+",\"type\":2}",u.getAuroraAlias());
//					AuroraPush.pushTargetForDriver(u.getUseEquipment(), "{\"orderId\":"+startRemark.getOrderId()+",\"type\":2}",u.getAuroraAlias());
//					AuroraPush.pushTargetAlertForDriver(u.getUseEquipment(), "You received a 3-star review.");
				   UserMessage um = new UserMessage();
				   um.setCreateTime(new Date());
				   um.setmContext(startRemark.getContent());
				   um.setmName("You received a 3-star review.");
				   um.setmSummary("You received a 3-star review.");
				   um.setType(1);
				   um.setuId(oi.getDirberId());
				   userMessageMapper.insertSelective(um);
				}
			}
			if(code!=200){
				TransactionAspectSupport.currentTransactionStatus().isRollbackOnly();
			}
			return new ReturnData(code,msg);
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

	@Override
	public ReturnData getList() {
		Map<String,Object> map = new HashMap<>();
		PageHelper.startPage(0, 10);
		 List<StarRemark>  list=starRemarkMapper.getStarRemarkList1(map);
		 
		return new ReturnData(list);
	}

}
